<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            三方库发货验证
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 发货单号" id="consignment" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/输入 托号" id="trayyinfoid" value="" onclick="getIdSelector();"
                           >
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">条码</div>
                    <input type="text" class="aui-input" id="tm">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <input type="text" class="aui-input" id="mrlCode" disabled="true">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:60%">总箱量</div>
                    <input type="text" class="aui-input" id="number"  disabled="true">
                </div>
                <div class="aui-list-item-inner" style="margin-left:-5%">
                    <div class="aui-list-item-label"  style="width:100%">已验证箱数</div>
                    <input type="text" class="aui-input" id="trueNumber"  disabled="true">
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" id="sure" >验证</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'>序号</td>
                        <td algin='center'>验证状态</td>
                        <td algin='center'>箱号</td>
                        <td algin='center'>托码</td>
                        <td algin='center'>批次号</td>
                        <td algin='center'>物料号</td>
                        <td algin='center'>物料名</td>
                        <td algin='center'>数量</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化
    apiready = function() {

        $("#consignment").focus();
        idSelector = "consignment";
        console.log("123");
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //1：发货单号
            if (idSelector == 'consignment') {
                
            $("#consignment").blur();

                //发货单号
                $("#consignment").val(ret);

                //查询表格数据
                search(ret);

                //2:箱号
            } else if (idSelector == 'boxnum') {
                $("#boxnum").blur();

                //条码解析+自栋绑定
                tmSplit(ret);
            }else if (idSelector == 'trayyinfoid') {
                $("#trayyinfoid").blur();
                //发货单号赋值
                $("#trayyinfoid").val(ret);
                bind();
            }
        });
    }

     //托号验证
     function bind() {
        console.log("11;777")
        //发货单号
        var consignment = $("#consignment").val();
        //托码
        var trayyinfoid = $("#trayyinfoid").val();

        var boxnum = $("#boxnum").val();

        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 5000
            });
            return;
        }
        //托码
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描托码！',
                duration: 5000
            });
            return;
        }else if(boxnum!="" && boxnum != null && boxnum != undefined){
            api.toast({
                msg: '仅支持扫箱或扫托验证，请重新扫描！',
                duration: 5000
            });
            $("#trayyinfoid").val("");
            $("#boxnum").val("");
            return;
        }

        api.showProgress({
            title: '验证中...',
        });
console.log("11111")
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKficition";
        var data = {
            values: {
                consignment: consignment, //发货单号
                trayCode: trayyinfoid, //托号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    // api.toast({
                    //     msg: '验证成功',
                    //     duration: 5000
                    // });
                    var div = document.getElementById("sure")
                    div.innerText= myobj.message;
                    //清空表单
                    $("#trayyinfoid").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    search(consignment);
                    //失败
                } else if (myobj.code == '1') {
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector="trayyinfoid";

                    api.hideProgress();
                   // $("#trayyinfoid").val("");
                    // api.toast({
                    //     msg: myobj.message,
                    //     duration: 5000
                    // });
                    var div = document.getElementById("sure")
                    div.innerText= myobj.message;
                   // $("#trayyinfoid").focus();
                }
            }else{
                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                 idSelector="trayyinfoid";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }

        });
    }
    //条码拆分
    function tmSplit(tm) {
        //条码解析
        var arr = tm.split(",");
        //物料号
        $("#mrlCode").val(arr[1]);
        //数量
        $("#qty").val(arr[2]);
        //箱号
        $("#boxnum").val(arr[9]);
        //条码
        $("#tm").val(tm);

        //发货单选中
        $("#boxnum").select();

        //自动验证
        verification();
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if(tm != null && tm != "" && tm != undefined){
                $("#boxnum").blur();
            console.log(tm);

            //条码拆分+自动验证
            tmSplit(tm);
            }
            
        }
    });

       //托码监听回车事件
       $("#trayyinfoid").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var trayyinfoid = $(this).val();
        if (keycode == '13') {
            if(trayyinfoid != null && trayyinfoid != "" && trayyinfoid != undefined){
                $("#trayyinfoid").blur();
            console.log(trayyinfoid);
            
            $("#trayyinfoid").val(trayyinfoid);
            bind();
            }
            
        }
    });
    //发货单号监听回车事件
    $("#consignment").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {
            if(consignment != null && consignment != "" && consignment != undefined){
            //     $("#consignment").blur();
            // console.log(tm);
            //物料号
            $("#consignment").val(consignment);

            //发货单选中
           // $("#consignment").select();


            //查询表格数据
            search(consignment);
            }
           
        }
    });

    //验证按钮
    function verification() {
        //发货单号
        var consignment = $("#consignment").val();
        //箱号
        var boxnum = $("#boxnum").val();

        var trayyinfoid = $("#trayyinfoid").val();
        //条码
        var tm = $("#tm").val();
        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 3000
            });
            return;
        }

        //箱号
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描箱号',
                duration: 3000
            });
            return;
        } else if(trayyinfoid!= "" && trayyinfoid != null && trayyinfoid != undefined){
            api.toast({
                msg: '仅支持扫箱或扫托验证，请重新扫描！',
                duration: 5000
            });
            $("#trayyinfoid").val("");
            $("#boxnum").val("");
            return;
        }
        
        api.showProgress({
            title: '验证中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKVerificationg";
        var params = new Object();
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment, //发货单号
                tm: tm, //条码
                boxnum: boxnum, //箱号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        },function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    // api.toast({
                    //     msg: '验证成功',
                    //     duration: 3000
                    // });
                    var div = document.getElementById("sure")
                    div.innerText= myobj.message;
                    //清空表单
                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");

                    //查询表格数据
                    search(consignment);

                    //失败
                } else if (myobj.code == '1') {
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector="boxnum";
                    api.hideProgress();
                    //$("#boxnum").val("");
                    // api.toast({
                    //     msg: myobj.message,
                    //     duration: 3000
                    // });
                    var div = document.getElementById("sure")
                    div.innerText= myobj.message;
                    //$("#boxnum").focus();
                }
            }else{
                $("#boxnum").val("");
                $("#boxnum").select();
                 idSelector="boxnum";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //查询表格数据
    function search(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKFhSearch";
        var params = new Object();
        params.url = url
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment, //发货单号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        },function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = myobj.list;
                  
                    //表格数据大于0

                    var html = "";
                    var trueNumber = 0;
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 11px;'>";
                        html += "<td name='seqNo'>" + (i + 1) + "</td>";
                        console.log(list[i].fhStatus);
                        if (list[i].fhStatus == '已验证') {
                            html += "<td name='fhStatus' style='background-color:green;width:80px;'> " + list[i].fhStatus + "</td>";
                            trueNumber++;
                            console.log(trueNumber);
                            } else {
                                html += "<td name='fhStatus' style='background-color:red;width:80px;'> " + list[i].fhStatus + "</td>";
                            }
                        html += "<td name='boxnum'>" + list[i].boxnum + "</td>";
                        if (list[i].ctBarcode == undefined||null) {
                            html += "<td name='ctBarcode'>" + "" + "</td>";
                            } else {
                            html += "<td name='ctBarcode'>" + list[i].ctBarcode + "</td>";
                            }
                        // html += "<td name='ctBarcode'>" + list[i].ctBarcode + "</td>";
                        html += "<td name='fhStatus'>" + list[i].lotCode + "</td>";
                        // html += "<td name='lotCode'>" + list[i].lotCode  + "</td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";
                        html += "</tr>";
                    }
                    $("#number").val(list.length);
                    console.log(trueNumber);
                    $("#trueNumber").val(trueNumber);
                    $api.html($api.byId("tbodyHtml"), html);
                    
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                } else if (myobj.code == '1') {
                    $("#consignment").val("");
                    $("#consignment").select();
                    idSelector="consignment";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }else{
                $("#consignment").val("");
                $("#consignment").select();
                 idSelector="consignment";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }
        });
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
